package com.bobo.netty.service.impl;

import com.bobo.netty.common.bizEnum.MessageStatusEnum;
import com.bobo.netty.dao.chat.ChatMessageMapper;
import com.bobo.netty.dao.chat.po.ChatMessagePO;
import com.bobo.netty.dto.ChatMessageDTO;
import com.bobo.netty.server.websocketchat.RedisProcess;
import com.bobo.netty.service.ChatMessageService;
import com.bobo.netty.utils.IDUtils;
import com.bobo.netty.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChatServiceImpl.class);
    @Resource
    private ChatMessageMapper chatMessageMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RedisProcess redisProcess;
    @Override
    @Async("nettyServerThreadPool")
    public void saveMsgSingle(ChatMessageDTO chatMessageDTO) {
        ChatMessagePO record = new ChatMessagePO();
        record.setMessageId(IDUtils.getUUid());
        record.setContent(chatMessageDTO.getContent());
        record.setSendUid(chatMessageDTO.getSendUserId());
        record.setSendUname(redisProcess.getUserName(record.getSendUid()));
        record.setRecUid(chatMessageDTO.getReceiveUserId());
        record.setRecUname(redisProcess.getUserName(record.getRecUid()));
        if (MessageStatusEnum.ONLINE_MSG.getCode().equals(chatMessageDTO.getMessageStatus())) {
            record.setSendState(1);
        }else {
            record.setSendState(0);
        }
        int insert = chatMessageMapper.insert(record);
        LOGGER.info("消息插入结果：" + insert + "id=" + record.getId());
    }
}
